package com.sxhuayuan.parking.plugin.parking.che_an.request;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.tomcat.util.buf.StringUtils;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.exception.SystemException;
import com.sxhuayuan.parking.plugin.parking.AbstractRequest;
import com.sxhuayuan.parking.utils.DateUtils;

public class CheAnBaseRequest extends AbstractRequest {

	@NotBlank
	private String from;
	@NotBlank
	private String timestamp;
	@NotBlank
	private String nonce;
	@NotBlank
	private String sign;
	@NotBlank
	private String branchno;
	@NotBlank
	private CheAnBaseData data;

	private QueryFormat queryFormat;

	public class QueryFormat {
		private Integer rowindex;

		private Integer pagesize;

		public Integer getRowindex() {
			return rowindex;
		}

		public void setRowindex(Integer rowindex) {
			this.rowindex = rowindex;
		}

		public Integer getPagesize() {
			return pagesize;
		}

		public void setPagesize(Integer pagesize) {
			this.pagesize = pagesize;
		}

	}

	public CheAnBaseRequest() {
		this.nonce = RandomStringUtils.randomNumeric(2);
		this.timestamp = DateUtils.format();
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getBranchno() {
		return branchno;
	}

	public void setBranchno(String branchno) {
		this.branchno = branchno;
	}

	public CheAnBaseData getData() {
		return data;
	}

	public void setData(CheAnBaseData data) {
		this.data = data;
	}

	public QueryFormat getQueryFormat() {
		return queryFormat;
	}

	public void setQueryFormat(QueryFormat queryFormat) {
		this.queryFormat = queryFormat;
	}

	@Override
	protected void checkConstraints() {
		// TODO Auto-generated method stub

	}

	@Override
	public void checkAndSign(boolean ignoreEmptyValue, boolean dictionarySort, String suffix, String key) throws SystemException {
		List<String> list = new ArrayList<>(4);
		list.add(this.nonce);
		list.add(this.timestamp);
		list.add(this.from);
		Collections.sort(list);
		String str = StringUtils.join(list) + JSON.toJSONString(data);
		try {
			Mac mac = Mac.getInstance("HMACSHA1");
			byte[] secretByte = key.getBytes("UTF-8");
			byte[] dataBytes = str.getBytes("UTF-8");
			SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA1");
			mac.init(secret);
			byte[] doFinal = mac.doFinal(dataBytes);
			this.sign = Base64.getEncoder().encodeToString(doFinal);
		} catch (Exception e) {
			log.error("", e);
		}
	}

}
